--[[	$Id: caelThreat.lua 2610 2012-09-25 15:13:02Z sdkyron@gmail.com $	]]

local _, caelThreat = ...

local abs = math.abs
local playerClass = caelLib.playerClass
local unitClass, lastWarning

caelThreat.eventFrame = CreateFrame("Frame", nil, self)

local warningSounds = true

local isTankClassSpec = {
	["DEATHKNIGHT"] = GetSpellInfo(93099), -- Vengeance -- GetSpellInfo(48263), -- Blood Presence
	["DRUID"] = GetSpellInfo(84840), -- Vengeance -- GetSpellInfo(5487), -- Bear Form
	["MONK"] = GetSpellInfo(120267), -- Vengeance
	["PALADIN"] = GetSpellInfo(84839), -- Vengeance
	["WARRIOR"] = GetSpellInfo(93098), -- Vengeance
--[[
	["PALADIN"] = {
		GetSpellInfo(25780), -- Righteous Fury
		(GetSpellInfo(465)), -- Devotion Aura
	},
	["WARRIOR"] = function(unit)
		return UnitGroupRolesAssigned(unit) == "TANK"
	end
--]]
}

local function IsTankCheck(unit, check)
	local status = false
	_, unitClass = UnitClass(unit)

	if type(check) == "table" then
		status = true
		for i = 1, #check do
			if not UnitAura(unit, check[i]) then
				status = false
			end
		end
	elseif type(check) == "function" then
		if isTankClassSpec[unitClass](unit) then
			status = true
		end
	elseif check then
		if UnitAura(unit, check) then
			status = true
		end
	end

	return status
end


local aggroColors = {
	[true] = {
		[1] = {1, 0.6, 0, 1},
		[2] = {1, 1, 0.47, 1},
		[3] = {0.33, 0.59, 0.33, 1},
	},
	[false] = {
		[1] = {1, 1, 0.47, 1},
		[2] = {1, 0.6, 0, 1},
		[3] = {0.69, 0.31, 0.31, 1},
	}
}

caelThreat.eventFrame:RegisterEvent("UNIT_AURA")
caelThreat.eventFrame:RegisterEvent("GROUP_ROSTER_UPDATE")
caelThreat.eventFrame:RegisterEvent("UNIT_THREAT_LIST_UPDATE")
caelThreat.eventFrame:RegisterEvent("UNIT_THREAT_SITUATION_UPDATE")
caelThreat.eventFrame:HookScript("OnEvent", function(self, event, unit)
	if tostring(GetZoneText()) == "Wintergrasp" or QueueStatusMinimapButton.status == "active" then return end

	if not unit then return end

	_, unitClass = UnitClass(unit)

	local unitIsTank = IsTankCheck(unit, isTankClassSpec[unitClass])
	local playerIsTank = IsTankCheck("player", isTankClassSpec[playerClass])

	if event ~= "UNIT_AURA" then

		local _, status, threatPercent = UnitDetailedThreatSituation("player", "target")

		if not playerIsTank then

			if status then
				threatPercent = floor(threatPercent + 0.5)
			end

			if (status and status < 1)	then
				if (abs(threatPercent - 20) <= 5) then
					if (lastWarning ~= 20) then
						RaidNotice_AddMessage(RaidWarningFrame, "|cff559655".."~20% THREAT|r", ChatTypeInfo["RAID_WARNING"])
						lastWarning = 20
					end
				elseif (abs(threatPercent - 40) <= 5) then
					if (lastWarning ~= 40) then
						RaidNotice_AddMessage(RaidWarningFrame, "|cff559655".."~40% THREAT|r", ChatTypeInfo["RAID_WARNING"])
						lastWarning = 40
					end
				elseif (abs(threatPercent - 60) <= 5) then
					if (lastWarning ~= 60) then
						RaidNotice_AddMessage(RaidWarningFrame, "|cffFFFF78".."~60% THREAT|r", ChatTypeInfo["RAID_WARNING"]) -- Yellow |cffA5A05A
						lastWarning = 60
					end
				end
			elseif (status and status > 0 and status < 3 and unit == "player") then
				if (abs(threatPercent - 80) <= 5) then
					if (lastWarning ~= 85) then
						if warningSounds then
							PlaySoundFile(caelMedia.files.soundWarning)
						end
						RaidNotice_AddMessage(RaidWarningFrame, "|cffFF9900".."WARNING THREAT: "..tostring(threatPercent).."%|r", ChatTypeInfo["RAID_WARNING"]) -- Orange |cffB46E46
						lastWarning = 85
					end
				end
			elseif (status and status > 2 and unit == "player") then
				if warningSounds then
					PlaySoundFile(caelMedia.files.soundAggro)
				end
				RaidNotice_AddMessage(RaidWarningFrame, "|cffAF5050AGGRO|r", ChatTypeInfo["RAID_WARNING"]) -- Red
				UIFrameFlash(caelCoreModuleShadowEdge and caelCoreModuleShadowEdge or LowHealthFrame, 0.2, 0.2, 0.4, caelCoreModuleShadowEdge and true or false, 0, 0.2)
			end
		end
	end

	if GetNumGroupMembers() > 0 then

		if IsAddOnLoaded("caelPanels") then
			for _, panel in pairs(caelPanels) do
				local status = UnitThreatSituation("player")

				if (status and status > 0) then
					local r, g, b = unpack(aggroColors[playerIsTank][status])
					panel:SetBackdropBorderColor(r, g, b)
				else
					panel:SetBackdropBorderColor(0, 0, 0)
				end
			end
		end

		if IsAddOnLoaded("oUF_Caellian") then
			for k,v in pairs(oUF.objects) do
				if v.unit == unit then
					local status = UnitThreatSituation(unit)

					if (status and status > 0) then
						local r, g, b = unpack(aggroColors[unitIsTank][status])
						v.FrameBackdrop:SetBackdropColor(r, g, b, a)
						if v.Overlay then
							v.Overlay:SetStatusBarColor(r, g, b, a)
						end
					else
						v.FrameBackdrop:SetBackdropColor(0, 0, 0, 0)
						if v.Overlay then
							v.Overlay:SetStatusBarColor(0.1, 0.1, 0.1, 0.75)
						end
					end
				end
			end					
		end
	end
end)